// 检测H5或PC
export const isMobile = () => {
  let ua = navigator.userAgent.toLowerCase();
  return /iphone|ipad|ipod|android|blackberry|windows phone/.test(ua);
}

// 计算像素单位
export const transUnit = (val: number | string, rem: Boolean, baseFontSize: number = 16) => {
  const num = typeof val === "string" ? parseFloat(val) : val;
  if (isNaN(num)) return val; // 如果无法转换为数字，返回原值
  if (rem) return `${num / baseFontSize}rem`;
  return `${num}px`;
};

// 处理src路径
export const publicSrc = (src: string): string => {
  if (!src) return '';
  return src.includes('http') ? src : `${import.meta.env.VITE_BASE_URL}${src}`;
}

// 数字滚动
export const animateNumber = (start: number, end: number, duration: number, callback: (num: number) => void) => {
  const startTime = performance.now();
  const updateNumber = (currentTime: number) => {
    const elapsedTime = currentTime - startTime;
    const progress = Math.min(elapsedTime / duration, 1);
 
    let num = Math.floor(start + (end - start) * progress);
    callback(num)
    if (progress < 1) {
      requestAnimationFrame(updateNumber);
    }
  };
  requestAnimationFrame(updateNumber);
}
